/**
 * GenericDaoHibernateImpl.java
 * cn.feigme.dao.impl
 * @author Feigme 
 * 2007-8-26
 */
package cn.feigme.base.dao.impl;

import java.io.Serializable;

import cn.feigme.base.dao.IGenericDao;
import cn.feigme.util.GenericsUtils;

/**
 * @author Feigme 
 *
 */
public abstract class GenericDaoHibernateImpl<T, PK extends Serializable> extends BaseDao implements IGenericDao<T,PK> {

	private Class<T> persistentClass;
	
	/**
	 * method 3
	 * 提供空的构造函数,自动获得DAO的persistentClass
	 */
	@SuppressWarnings("unchecked")
	public GenericDaoHibernateImpl(){
		this.persistentClass = GenericsUtils.getSuperClassGenricType(getClass());
	}
	
	/**
	 * method 2
	 * 初始化DAO，获取POJO类型
	 */
//	@SuppressWarnings("unchecked")
//	public GenericDaoHibernateImpl(Class<T> t) {
//		this.persistentClass = (Class<T>) ((ParameterizedType) getClass()   
//                .getGenericSuperclass()).getActualTypeArguments()[0];  
//	}
	
	/**
	 * method 1
	 */
//	public GenericDaoHibernateImpl(Class<T> t){
//		this.persistentClass = t;
//	}
	
	public Class<T> getPersistentClass() {   
	    return persistentClass;   
	}

	public void delete(T persistentObject) {
		
		this.getHibernateTemplate().delete(persistentObject);
	}

	@SuppressWarnings("unchecked")
	public T find(PK id) {
		
		return (T) this.getHibernateTemplate().get(getPersistentClass(), id);
	}

	@SuppressWarnings("unchecked")
	public PK save(T newInstance) {
		
		return (PK)this.getHibernateTemplate().save(newInstance);
	}

	public void update(T transientObject) {
		
		this.getHibernateTemplate().update(transientObject);
	}

}
